import jsonpath
import requests
from faker import Faker


class BaseApi:
    def __init__(self):
        self.base_url = 'http://litemall.hogwarts.ceshiren.com/'
        self.token = self.get_token()
        self.faker = Faker("zh_CN")
        self.name = self.faker.name()
        self.new_name = self.faker.name()
    # 封装获取token方法
    def get_token(self):
        info={
            "url":f'{self.base_url}/admin/auth/login',
            "method":"POST",
            "json":{"username":"user123","password":"user123"}
        }
        r = self.request(**info)
        token = jsonpath.jsonpath(r.json(),expr="$..token")[0]
        return token
    # 封装接口方法
    def request(self,**kwargs):
        r = requests.request(**kwargs)
        return r